﻿Public Class MultichoiceControl

    Dim timeleft As Integer
    Dim totaltime As Integer
    Dim hastimelimit As Boolean = False
    Event AnswerGiven(ByVal Answer As MPQService.Answer)
    Event OutOfTime()

    Sub New()
        InitializeComponent()
    End Sub

    Const QuestionNumberStart = "Question Number {0}"

    Sub NewQuestion(ByVal Question As MPQService.Question)
        Me.QuestionNumber.Text = System.String.Format(QuestionNumberStart, Question.QuestionID)
        Me.QuestionText.Text = Question.QuestionText
        If Question.TimeLimit = 0 Then
            hastimelimit = False
            TimeRemaining.Visible = False
            TimeRemainingLabel.Visible = False
        Else
            hastimelimit = True
            timeleft = Question.TimeLimit * 10
            totaltime = Question.TimeLimit * 10
            TimeRemaining.Visible = True
            TimeRemainingLabel.Visible = True
            TimeRemainingLabel.BringToFront()
        End If
        AnswerA.Text = Question.A
        AnswerB.Text = Question.B
        AnswerC.Text = Question.C
        AnswerD.Text = Question.D
    End Sub

    Private Sub AnswerA_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AnswerA.Click
        RaiseEvent AnswerGiven(MPQService.Answer.A)
    End Sub
    Private Sub AnswerB_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AnswerB.Click
        RaiseEvent AnswerGiven(MPQService.Answer.B)
    End Sub
    Private Sub AnswerC_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AnswerC.Click
        RaiseEvent AnswerGiven(MPQService.Answer.C)
    End Sub
    Private Sub AnswerD_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AnswerD.Click
        RaiseEvent AnswerGiven(MPQService.Answer.D)
    End Sub

    Private Sub SecondTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SecondTimer.Tick
        TimeRemaining.Value = timeleft / totaltime * 100
        TimeRemainingLabel.Text = "Time Remaining: " & CInt(timeleft / 10)
        timeleft -= 1
        If timeleft <= -1 Then
            SecondTimer.Stop()
            RaiseEvent OutOfTime()
        End If
    End Sub

    Public Sub Start()
        SecondTimer.Start()
    End Sub

    Private Sub MultichoiceControl_CorrectAnswerEvent() Handles Me.AnswerGiven
        SecondTimer.Stop()
    End Sub
End Class
